package com.tanhua.management.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.management.vo.CommentVo;
import com.tanhua.management.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentService {

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;

    @Autowired
     private UserInfoService userInfoService;
    /**
     * 评论列表翻页
     * @param sortProp  排序字段
     * @param page
     * @param pageSize
     * @param sortOrder ascending 升序 descending 降序
     * @param messageID 消息ID
     * @return
     */
    public PageResult queryComment(String sortProp, Integer page, Integer pageSize, String sortOrder, String messageID) {
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        //查询mongodb中的Comment表中数据
        PageInfo<Comment> pageInfo = this.quanZiApi.queryComment(sortProp, page, pageSize, sortOrder, messageID);
        List<Comment> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }
        //查询用户信息
        List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>().in("user_id", userIdList);
        //查询mysql中的UserInfo表中数据
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByList(queryWrapper);
        if (CollUtil.isEmpty(userIdList)) {
            return pageResult;
        }
        //整合records，userInfoList数据到commentVoList中
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment record : records) {
            CommentVo commentVo = new CommentVo();
            commentVo.setId(record.getId().toHexString()); //编号id
            commentVo.setContent(record.getContent());//评论内容
            //转换时间格式
            commentVo.setCreateDate(DateUtil.format(new Date(record.getCreated()), "HH:mm"));//评论时间
            commentVo.setUserId(record.getUserId() + ""); //评论人id
            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equal(record.getUserId(), userInfo.getUserId())) {
                    //将用户在UserInfo表的其余数据封装进CommentVo表中
                    commentVo.setNickname(userInfo.getNickName()); //评论人昵称
                    break;
                }
            }
            commentVoList.add(commentVo);
        }
        pageResult.setItems(commentVoList);
        return pageResult;
    }
}
